package m2;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        // 新建消费者
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("c2");
        // 设置 name server
        c.setNamesrvAddr("192.168.64.141:9876");
        // 订阅消息
        // Tag1
        // Tag1 || Tag2 || Tag3
        // *
        c.subscribe("Topic2", "*");
        // 设置监听器，处理消息
        // Orderly - 使用一个线程，顺序处理消息
        c.setMessageListener((MessageListenerOrderly) (msgs, context) -> {
            for (MessageExt ext : msgs) {
                String s = new String(ext.getBody());
                System.out.println("收到： "+s);
            }
            return ConsumeOrderlyStatus.SUCCESS;
            //return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
        });
        // 启动
        c.start();
    }
}